<template>
  <div
    class="dv-gui g-select"
    :class="[
      {
        'is-inline': !!inline,
        'is-single': inline === 'inline-single',
        'is-disabled': disabled,
      }
    ]"
  >
    <b-select
      :model-value="modelValue"
      :size="size"
      :disabled="disabled"
      @update:model-value="handleInput"
      @change="handleChange"
    >
      <b-option-group
        v-for="item in data"
        :key="item.group"
        :label="item.group"
      >
        <b-option
          v-for="item in item.data"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span>{{ item.label }}</span>
          <span style="float:right;color:#666">{{ item.value }}</span>
        </b-option>
      </b-option-group>
    </b-select>
    <span v-if="label" class="g-input__caption">
      {{ label }}
    </span>
  </div>
</template>

<script>
export default {
  name: 'GGroupSelect',
  props: {
    modelValue: {
      type: [String, Number],
      default: 0,
    },
    data: {
      type: Array,
      default: () => [],
    },
    label: {
      type: String,
      default: '',
    },
    size: {
      type: String,
      default: 'small',
    },
    inline: {
      type: [Boolean, String],
      default: false,
    },
    disabled: Boolean,
  },
  emits: ['update:modelValue', 'change'],
  setup(props, ctx) {
    const handleInput = (value) => {
      ctx.emit('update:modelValue', value)
    }

    const handleChange = (currentValue) => {
      ctx.emit('change', currentValue)
    }

    return {
      handleInput,
      handleChange,
    }
  },
}
</script>

<style scoped lang="scss">
.dv-gui {
  &.g-select {
    width: 100%;
    margin-bottom: 4px;
  }
  &.is-inline {
    display: inline-block;
    width: 50%;
    padding-right: 8px;
  }
  &.is-single {
    display: inline-block;
    width: 100%;
    padding-right: 8px;
  }
}
</style>
